package server.annotationn;


import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;
import java.util.Objects;

@Aspect
@Component
@SuppressWarnings("unused")
public class ParamVerityAspect {

    @Pointcut("@annotation(ParamVerity)")
    public void paramPointCut() {
    }

    @Around("paramPointCut()")
    public Object paramAfterReturn(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        ParamVerity paramVerity = signature.getMethod().getAnnotation(ParamVerity.class);
        String[] names = paramVerity.name();
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        Map<String, String[]> parameterMap = request.getParameterMap();
        int i = 0;
        int len = names.length;
        while (i < len) {
            String name = names[i];
            if (StrUtil.isNotEmpty(name)) {
                String param = request.getParameter("name");
            } else {
                break;
            }
            i += 1;
        }
        return point.proceed();
    }

}
